import query from "./urlQuery";

const params_obj = query(window.location.href);
let company = params_obj["u"];

// 查找数组中对象的key等于某个值的唯一对象
export const key_find_obj = (str, arr, key) => {
  let result = arr.filter((item) => {
    return item[key] === str;
  });
  if (result.length === 1) {
    return result[0];
  } else {
    return {
      label:str,
      msg_en: str,
      msg_cn: str,
    };
  }
};

// 查找数组中对象的key等于某个值的唯一对象的显示值
export const key_find_show_value = (str, arr, key) => {
  let result = arr.filter((item) => {
    return item[key] === str;
  });
  if (result.length === 1) {
    return company === "mg" ? result[0].msg_en : result[0].msg_cn;
  } else {
    return {
      msg_en: str,
      msg_cn: str,
    };
  }
};
// 查找对象中的对象的key等于某个值的唯一对象的显示值
export const key_find_check_value = (str, obj, key) => {
  let val_arr = [];
  for (let key in obj) {
    val_arr.push(obj[key]);
  }
  let result = val_arr.filter((item) => {
    return item[key] === str;
  });
  if (result.length === 1) {
    return company === "mg" ? result[0].msg_en : result[0].msg_cn;
  } else {
    return str;
  }
};
